//
// Created by v on 19-6-27.
//

#include <stdlib.h>
#include <unistd.h>

#include "CMMqttMosquitto.h"
#include "utils/cm_common.h"
#include "utils/cm_logprint.h"

CMLogPrint logPrint;

bool connected = false;
class CMMqttRecvFunc : public CMMqttFunction
{
  public:
    void onLog(int level, const char *str) CM_CPP_OVERRIDE
    {
        cminfo("[%d][%s]\n", level, str);
    }

    void onPublish(int id, int resultCode) CM_CPP_OVERRIDE
    {
        cminfo("id=%d, resultcode = %d\n", __func__, __LINE__, id, resultCode);
    }

    void onMessage(int id, const char *topic, const char *data,
                   size_t datalen) CM_CPP_OVERRIDE
    {
        cminfo("id=%d, topic=%s, data=[%d][%s]\n", id, topic, datalen, data);
    }

    void onConnect(int result) CM_CPP_OVERRIDE
    {
        cminfo("connected result [%d]\n", result);
    }

    void onDisconnect(int result) CM_CPP_OVERRIDE
    {
        cminfo("disconnected [%d]\n", result);
        connected = false;
    }

    void onSubscribe(int id) CM_CPP_OVERRIDE { cminfo("id=%d\n", id); }
};

void snet_test(const char *host, int port)
{
    cminfo("snet with host = [%s] port=[%d]\n", host, port);
    CMMqttBase *    mqtt = new CMMqttMosquitto(host, port);
    CMMqttFunction *rf   = new CMMqttRecvFunc();
    rf->setHandler(mqtt);
    mqtt->setDebug(true);
    mqtt->setFunction(rf);

//    mqtt->setTls("/opt/data/proj/dev/jklibs/weixin", NULL, NULL, NULL);
    // mqtt->setTls("/etc/weixin", NULL, NULL, NULL);
//     mqtt->setTlsInsecure();

    mqtt->setId("test-device");
    // mqtt->setId("CMCC-500534-D8B6C103058");
    int ret = -1;
    cminfo("start to init ...\n");
    ret = mqtt->init();
    if (ret < 0)
    {
        cmerror("init fail [%d]\n", ret);
        return;
    }
    else
    {
        cminfo("mqtt init success ret [%d]\n", ret);

        mqtt->setUsername("", "");
    }
    ret = mqtt->connect();
    if (ret < 0)
    {
        cmerror("connect fail [%d]\n", ret);
    }
    else
    {
        connected = true;
        cminfo("mqtt connect success\n");
    }

    int count = 0;
    while (true)
    {
        if (!connected)
        {
            ret = mqtt->reconnect();
            if (ret < 0)
            {
                cmerror("connect fail [%d]\n", ret);
                sleep(1);
                continue;
            }
            else
            {
                connected = true;
                cminfo("mqtt connect success\n");
            }
        }
        if (count == 3)
        {
            cminfo("to sub\n");
            mqtt->sub("/device_status/offline", 1);
        }
        if (count % 5 == 0)
        {
            cminfo("to pub\n");
            mqtt->pub("/device_status/online", "hello body", 0);
        }
        if (count == 55)
            break;
        mqtt->loop();
        count++;
        sleep(1);
    }
    cminfo("mqtt goto disconnect\n");
    mqtt->disconnect();
    mqtt->deinit();
    delete mqtt;
    cminfo("mqtt done, exit\n");
}

int main(int argc, char **args)
{
    const char *host = args[1];
    int         port = strtol(args[2], NULL, 10);

    CM_LOG_PRINT_INIT("snet");
    cm_mqtt_init_all();
    snet_test(host, port);

    cm_mqtt_deinit_all();
    getchar();
    return 0;
}
